namespace soccer.helper {
    import b2Vec2 = Box2D.Common.Math.b2Vec2;
    import b2BodyDef = Box2D.Dynamics.b2BodyDef;
    import b2Body = Box2D.Dynamics.b2Body;
    import b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
    import b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
    import b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
    export const p2m: number = 30;  //与现实世界的比例关系
    export const step: number = 60;

    //球拍的位置
    export type Pos = {
        x: number,
        y: number,
    }
    export function SamePos(p1, p2: Pos): boolean {
        if (p1 == null) {
            return p2 == null
        }
        if (p2 == null) {
            return p1 == null
        }
        return (p1.x == p2.x) && (p1.y == p2.y)
    }

    export function NewVec2(x, y: number): Box2D.Common.Math.b2Vec2 {
        return new Box2D.Common.Math.b2Vec2(x, y)
    }
    export function Distance(p1, p2: Box2D.Common.Math.b2Vec2): number {
        return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2))
    }
    export function Subtract(p1, p2: Box2D.Common.Math.b2Vec2): Box2D.Common.Math.b2Vec2 {
        let p = p1.Copy()
        p.Subtract(p2)
        return p
    }

    export function createBox(world: Box2D.Dynamics.b2World, posX: number, posY: number, w: number, h: number, isStatic: boolean = false): Box2D.Dynamics.b2Body {
        posX = posX / helper.p2m;
        posY = posY / helper.p2m;
        w = w / helper.p2m / 2;
        h = h / helper.p2m / 2;
        var bodyDef: Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
        bodyDef.position = helper.NewVec2(posX, posY);
        if (isStatic) {
            bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
        } else {
            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
        }

        var body: Box2D.Dynamics.b2Body = world.CreateBody(bodyDef);
        var poly: Box2D.Collision.Shapes.b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape.AsBox(w, h);

        var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
        fixtureDef.density = 1;
        fixtureDef.shape = poly;
        fixtureDef.friction = 1
        fixtureDef.restitution = 1

        body.CreateFixture(fixtureDef);
        return body
    }

    export function createCircle(world: Box2D.Dynamics.b2World, posX: number, posY: number, radius: number, isStatic: boolean = false, bodyDef: b2BodyDef = null, fixtureDef: b2FixtureDef = null): b2Body {
        posX = (posX) / helper.p2m;
        posY = (posY) / helper.p2m;
        radius = radius / helper.p2m;
        if (bodyDef == null) {
            bodyDef = new b2BodyDef();
            bodyDef.bullet = true
        }
        bodyDef.position = new b2Vec2(posX, posY);
        if (isStatic) {
            bodyDef.type = b2Body.b2_staticBody;
        } else {
            bodyDef.type = b2Body.b2_dynamicBody;
        }
        var body: b2Body = world.CreateBody(bodyDef);

        var poly: b2CircleShape = new b2CircleShape()
        //poly.SetLocalPosition(new b2Vec2(posX, posY))
        poly.SetRadius(radius)

        if (fixtureDef == null) {
            fixtureDef = new b2FixtureDef();
            fixtureDef.friction = 1
            fixtureDef.restitution = 1
            fixtureDef.density = 1;
        }
        fixtureDef.shape = poly;

        body.CreateFixture(fixtureDef);
        return body
    }
}